import { _decorator, Component, Event, instantiate, isValid, Label, Node, Prefab, ScrollView, UIOpacity, v3 } from 'cc';
import { BaseViewCmpt } from '../../Frame/Script/Base/baseViewCmpt';
import { ResLoadHelper } from '../../Frame/Script/resLoadHelper';
import { RankItem } from '../item/RankItem';
const { ccclass, property } = _decorator;

@ccclass('RankViewManager')
export class RankViewManager extends BaseViewCmpt {


    /**
     * 2:2人模式
     * 3:3人模式
     * 4:4人模式
     */
    private coomboType: number = 2;
    /**
     * 1：周排行
     * 2：总排行
     */
    private rankTitleType: number = 1;
    /**
     * 1:简单
     * 2：困难
     */
    private rankGradeType: number = 1;

    private rankItemList: Node[] = [];

    private rankBox: Node = null;
    private rankTypeLabel: Label = null;
    private img_rank_combo: Node = null;
    private rank_title_left: Node = null;
    private rank_title_right: Node = null;
    private easy_btn_node: Node = null;
    private difficult_btn_node: Node = null;
    private rankScrollView: ScrollView = null;
    private scrollContent: Node = null;


    protected onLoad(): void {
        super.onLoad();
        this.rankTitleType = 1;
        this.rankGradeType = 1;
        this.coomboType = 2;
        this.rankItemList = [];
        this.rank_title_left = this.viewList.get("content/rank_title/left");
        this.rank_title_right = this.viewList.get("content/rank_title/right");
        this.easy_btn_node = this.viewList.get("content/rank_title/btnEasy");
        this.difficult_btn_node = this.viewList.get("content/rank_title/btnDifficult");
        this.rankBox = this.viewList.get("content/rank_title/btnRankCom/img_rank_combobox");
        this.rankTypeLabel = this.viewList.get("content/rank_title/btnRankCom/rankTypeLabel").getComponent(Label);
        this.img_rank_combo = this.viewList.get("content/img_rank_combo");
        this.rankScrollView = this.viewList.get("content/ScrollView").getComponent(ScrollView);
        this.scrollContent = this.viewList.get("content/ScrollView/view/content");
    }

    start() {
        this.initContent();
        this.addRankItem();
    }

    update(deltaTime: number) {

    }

    private async addRankItem() {
        let pre: Prefab = await ResLoadHelper.loadAssetSync("prefabs/item/rankItem", Prefab);
        for (let i = 0; i < 100; i++) {
            let item = instantiate(pre);
            let grade = i + 1;
            item.name = "rank_" + grade;
            item.getComponent(RankItem).initItem(this.coomboType, grade, 99 - i);
            item.parent = this.scrollContent;
            this.rankItemList.push(item);
        }
        this.setScrollToTop();
    }

    private setScrollToTop() {
        if (this.rankScrollView.isScrolling || this.rankScrollView.isAutoScrolling) {
            this.rankScrollView.stopAutoScroll();
        }
        this.rankScrollView.scrollToTop();
    }

    private updateRankItem() {
        this.setScrollToTop();
        for (let i = 0; i < this.rankItemList.length; i++) {
            let item = this.rankItemList[i];
            /** @todo 获取对应的排行榜数据 */
            item.getComponent(RankItem).updateContent(this.coomboType, 99 - i);
        }
    }

    private initContent() {
        this.rankBox.setPosition(v3(0, -24, 0));
        this.img_rank_combo.active = false;
        this.rankTypeLabel.string = this.getComboStr();
        this.updateRankTitleTypeSelect(1);
        this.updateRankGradeTypeSelect(1);
    }

    private updateRankTitleTypeSelect(type: number) {
        this.rank_title_left.children[0].active = type === 1;
        this.rank_title_left.children[1].active = type === 2;
        this.rank_title_left.children[2].active = type === 1;
        this.rank_title_right.children[0].active = type === 2;
        this.rank_title_right.children[1].active = type === 1;
        this.rank_title_right.children[2].active = type === 2;
    }

    private updateRankGradeTypeSelect(type: number) {
        this.easy_btn_node.children[0].active = type === 2;
        this.easy_btn_node.children[1].active = type === 1;
        this.easy_btn_node.children[2].active = type === 1;
        this.difficult_btn_node.children[0].active = type === 1;
        this.difficult_btn_node.children[1].active = type === 2;
        this.difficult_btn_node.children[2].active = type === 2;
    }

    private getComboStr(): string {
        let str = "";
        let label = this.img_rank_combo.getChildByName("combo" + this.coomboType).getChildByName("label").getComponent(Label);
        str = label.string;
        return str;
    }

    private setComboSelect(type: number) {
        this.img_rank_combo.children.forEach(e => {
            let curType = Number(e.name.slice(-1));
            let selectNode = e.getChildByName("img_rank_combo_select");
            selectNode.active = curType === type;
        })
    }

    onClick_btnRankCom() {
        let posY = this.rankBox.position.y;
        this.rankBox.setPosition(v3(0, -posY, 0));
        this.img_rank_combo.active = !this.img_rank_combo.active;
        if (this.img_rank_combo.active) {
            this.setComboSelect(this.coomboType);
        }
    }

    onComboClicked(event) {
        let type = event.target.name.slice(-1);
        this.coomboType = Number(type);
        this.rankTypeLabel.string = this.getComboStr();
        let posY = this.rankBox.position.y;
        this.rankBox.setPosition(v3(0, -posY, 0));
        this.setComboSelect(this.coomboType);
        this.scheduleOnce(() => {
            this.img_rank_combo.active = false;
            this.updateRankItem();
        }, 0.01)
    }

    onWeekRankClicked() {
        this.rankTitleType = 1;
        this.updateRankTitleTypeSelect(this.rankTitleType);
        this.rankGradeType = 1;
        this.updateRankGradeTypeSelect(this.rankGradeType);
        this.updateRankItem();
    }

    onTotalRankClicked() {
        this.rankTitleType = 2;
        this.updateRankTitleTypeSelect(this.rankTitleType);
        this.rankGradeType = 1;
        this.updateRankGradeTypeSelect(this.rankGradeType);
        this.updateRankItem();
    }

    onEasyClicked() {
        this.rankGradeType = 1;
        this.updateRankGradeTypeSelect(this.rankGradeType);
        this.updateRankItem();
    }

    onDifficultClicked() {
        this.rankGradeType = 2;
        this.updateRankGradeTypeSelect(this.rankGradeType);
        this.updateRankItem();
    }
}


